Find the Frequency of Characters in a String in C Program

07-11-17 Course- C

In this program entered a string by user and a character and this program checks how many times that character is repeated in the string entered by user.

Source Code to Find the Frequency of Characters


#include <stdio.h>
int main(){
   char c[1000],ch;
   int i,count=0;
   printf("Enter a string: ");
   gets(c);
   printf("Enter a character to find frequency: ");
   scanf("%c",&ch);
   for(i=0;c[i]!='\0';++i)
   {
       if(ch==c[i])
           ++count;
   }
   printf("Frequency of %c = %d", ch, count);
   return 0;
}

Output


Enter a string: This website is awesome.
Enter a character to find frequency: e
Frequency of e = 4